package com.example.food.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.food.activity.StoreDetailActivity;
import com.example.food.bean.AlacarteBean;
import com.example.home.R;

import java.math.BigDecimal;
import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class Alacarteapter extends RecyclerView.Adapter<Alacarteapter.MyHoldy> {
    private Context context;
    private List<AlacarteBean.DataBean> list;
    private int counter = 0;

    public Alacarteapter(Context context, List<AlacarteBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }


    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_alacarte, null);
        return new Alacarteapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        AlacarteBean.DataBean data = list.get(position);
        if (list.get(position).getName() != null){
            Glide.with(context).load(ip + list.get(position).getImgUrl()).into(holder.imgUrl);
            holder.name.setText(list.get(position).getName());
            holder.saleQuantity.setText("月销售: " + String.valueOf(list.get(position).getSaleQuantity()));
            holder.favorRate.setText("好评率: " + String.valueOf(list.get(position).getFavorRate())+"%");
            holder.price.setText("价格: " + String.valueOf(list.get(position).getPrice()));
            holder.counterNum.setText(String.valueOf(list.get(position).getCounter()));

            // 在设置 counterNum 之后检查 counter 的值
            holder.counterNum.setText(String.valueOf(list.get(position).getCounter()));
            if (list.get(position).getCounter() > 0) {
                holder.reduce.setVisibility(View.VISIBLE);
            } else {
                holder.reduce.setVisibility(View.GONE);
            }

            holder.add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int oldCounter = data.getCounter();
                    data.setCounter(oldCounter + 1);
                    holder.counterNum.setText(String.valueOf(data.getCounter()));
                    setMoney();
                    // 检查计数器值并设置减法按钮的可见性
                    if (data.getCounter() > 0 && oldCounter == 0) {
                        holder.reduce.setVisibility(View.VISIBLE);
                        Alacarteapter.this.notifyItemChanged(position);
                    }
                }
            });

            holder.reduce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (data.getCounter() > 0) {
                        int oldCounter = data.getCounter();
                        data.setCounter(oldCounter - 1);
                        holder.counterNum.setText(String.valueOf(data.getCounter()));
                        setMoney();
                        // 在点击 reduce 后检查 counter 的值并设置 reduce 的可见性
                        if (data.getCounter() == 0 && oldCounter > 0) {
                            holder.reduce.setVisibility(View.GONE);
                            Alacarteapter.this.notifyItemChanged(position);
                        }
                    }
                }
            });



        }else {
            holder.name.setText("暂无数据");
        }


    }

    private void setMoney() {
        BigDecimal money = BigDecimal.ZERO;
        try {
            for (AlacarteBean.DataBean rows : list) {
                BigDecimal rowMoney = BigDecimal.valueOf(rows.getCounter()).multiply(BigDecimal.valueOf(rows.getPrice()));
                money = money.add(rowMoney);
            }

            if (context instanceof StoreDetailActivity) {
                StoreDetailActivity storeDetailActivity = (StoreDetailActivity) context;
                BigDecimal finalMoney = money;
                storeDetailActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        storeDetailActivity.getMoney(finalMoney);
                        Alacarteapter.this.notifyDataSetChanged();
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        ImageView imgUrl;
        TextView price;
        TextView favorRate;
        TextView name;
        RelativeLayout re;
        TextView saleQuantity;
        TextView reduce;
        TextView counterNum;
        TextView add;
        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            imgUrl = itemView.findViewById(R.id.imgUrl);
            name = itemView.findViewById(R.id.name);
            saleQuantity = itemView.findViewById(R.id.saleQuantity);
            favorRate = itemView.findViewById(R.id.favorRate);
            price = itemView.findViewById(R.id.price);
            re = itemView.findViewById(R.id.re);
            reduce = itemView.findViewById(R.id.reduce);
            counterNum = itemView.findViewById(R.id.counterNum);
            add = itemView.findViewById(R.id.add);
        }
    }
}
